import pageination from '../../components/pageination/pageination.vue'
export default {
	data() {
		return {
			//数据
      total: 0, //总条数
      page: 1, //当前页
      size: 10, //每页显示条数
      workLogTotal: 0, //工作记录总条数
      workLogPage: 1, //工作记录当前页
      workLogSize: 10, //工作记录每页显示条数

      search: '', //搜索信息
      modelShow: false,
      projectDetailsModelShow: false,
      projectDynamicModelShow: false,
      projectExpertModelShow: false,
			workModelShow: false,
      tempWorkLogProjectId: null,
      tempWorkLogExpertId: null,
			uploadNum: 0,
      validate: {}, //校验对象
      project: {}, //项目
      template: {}, //模板对象
      projectName: null, //项目名称
      areasList: [], //基地列表
      projectList: [], //项目列表
      expertWorkLogList: [], //专家日志列表
      uploadFileList: [], //上传文件列表
			 curId: 0,
			 curIndex:-1,//选中下标
		}
	},
	components: {
		pageination
	},
	created() { //调用方法
		let that = this;
    //that.searchTodo();
    that.searchAllAreas();
    that.getTemplate();
	},
	mounted() {
    let that = this;
    //校验名称重复
    $.validator.addMethod("checkNameRepeat", function(value, element, params) {
      let flg = false;
      if(that.project.id != null && (value == that.projectName)) {
        flg = true;
      } else {
        $.ajax({
          type: "POST",
          async: false,
          url: that.urls.projectCheckName,
          data: JSON.stringify({
            "name": value
          }),
          dataType: "json",
          contentType: "application/json",
          success: function(data) {
            if(data.success) {
              flg = !data.data;
            }
          }
        });
      }
      return this.optional(element) || flg;
    }, "该项目已存在！");

    that.validate = $("#projectForm").validate({
      ignore: [],
      rules: {
       /* areasId: { // 基地id
          required: true
        },*/
        name: { // 项目名称
          required: true,
          maxlength: 50
          //checkNameRepeat: true
        },
        type: { // 项目类型
          required: true
        },
        demandBookUrl: { // 需求书
          required: true,
          maxlength: 500
        },
        introduction: { // 项目简介
          required: true,
          maxlength: 1000
        }
      },
      messages: {
        /*areasId: { // 基地id
          required: "请选择基地！"
        },*/
        name: { // 项目名称
          required: "项目名称不能为空！",
          maxlength: "最多只能输入50个字！"
        },
        type: { // 项目类型
          required: "请选择项目类型！"
        },
        demandBookUrl: { // 需求书
          required: "请上传附件！",
          maxlength: "最多只能有500个字！"
        },
        introduction: { // 项目简介
          required: "项目简介不能为空！",
          maxlength: "最多只能输入1000个字！"
        }
      },
      /*errorElement: "em",
      errorPlacement: function(error, element) {
        error.appendTo(element.parents(".error-box").children(".error-msg"));
      }*/
    });

  },
	methods: { //写函数方法
		//给选中的列加背景色
		choosed:function(index){
			this.curIndex=index;
		},
		//页码切换执行方法
		pageFn: function(val, pid) {
			this.page = parseInt(val);
			//此处调列表数据接口
      this.searchTodo();
		},
    workLogPageFn: function(val, pid) {
      this.workLogPage = parseInt(val);
      //此处调列表数据接口
      this.searchAllExpertRorkRecord(this.tempWorkLogProjectId,this.tempWorkLogExpertId);
    },
		/**返回子组件用户选择的一页显示条数**/
		obtainChild: function(data) {
			this.row = data;
		},
		touchOffIdInput(n) {
			this.uploadNum = n;
			if(n == 1) {
				$("#handIdFront").click();
			}
		},
		/**文件上传js**/
		idFrontImg: function() {
      let that = this;
      let file = event.currentTarget.files[0];
      if (!that.fileGlobalFilter(file)) {
        return;
      }
      let formData = new FormData();
      formData.append('fileNames', file);
      let config = {
        headers: {
          'Content-Type': 'multipart/form-data;boundary = ' + new Date().getTime()
        }
      };
      that.$post(
        that.urls.upload,
        formData,
        config
      )
      .then(function(res) {
        if(res.success) {
          let fileInfo = res.data[0];
          that.project.demandBookUrl = fileInfo.url;
          $("input[name='demandBookUrl']").val(fileInfo.url);
          that.uploadFileList = [that.getFileName(fileInfo.url)];
          $("#demandBookUrl-error").css("display","none");
        }
      })
		},
		//打开项目需求模态框
		openModal: function(projectId) {
			//清空表单数据和校验信息
			$("#projectForm").validate().resetForm();
			$("input[type='reset']").trigger("click");
			this.project = {};
			this.uploadFileList = [];
			this.getProject(projectId);
			this.modelShow = true;
		},
		//项目详情模态框
		projectDetails: function(projectId) {
      this.getProject(projectId);
      this.projectDetailsModelShow = true;
		},
    //项目动态模态框
    projectDynamic: function(project) {
      this.project = project;
      this.projectDynamicModelShow = true;
    },
    //项目专家模态框
    projectExpert: function(project) {
      this.project = project;
		  this.projectExpertModelShow = true;
    },
		//工作记录
		rorkRecord: function(projectId,expertId) {

      this.workLogTotal = 0; //工作记录总条数
      this.workLogPage = 1; //工作记录当前页
      this.workLogSize = 10; //工作记录每页显示条数

		  //this.projectExpertModelShow = false;
			this.workModelShow = true;

      this.tempWorkLogProjectId = projectId;
      this.tempWorkLogExpertId = expertId;
			this.searchAllExpertRorkRecord(projectId,expertId);
		},
		//关闭模态框
		closeModel: function() {
      $(".secondLevelPageination .jump_page>input").val("");
		  this.modelShow = false;
      this.workModelShow = false;
      this.projectDetailsModelShow = false;
      this.projectDynamicModelShow = false;
      this.projectExpertModelShow = false;
      this.curId = 0;
    },
    closeWorkModel: function() {
		  this.workModelShow = false;
    },
    //预览
    preview: function(url,msg) {
      if (url == null || url == "") {
        if (msg == null || msg == "") {
          msg = "附件";
        }
        layer.msg("暂无" + msg + "！");
      } else {
        window.open(this.urls.baseUrl + url);
      }
    },
    preview2: function(url,msg) {
      if (url == null || url == "") {
        if (msg == null || msg == "") {
          msg = "附件";
        }
        layer.msg("暂无" + msg + "！");
      } else {
        window.open(url);
      }
    },
    //删除附件
    deleteEnclosure: function() {
      this.uploadFileList = [];
      this.project.demandBookUrl = "";
      $("input[name='demandBookUrl']").val("");
      $("#demandBookUrl-error").css("display","block");
    },
    //搜索数据列表
    searchTodo: function() {
      let that = this;
      that.$post(
        that.urls.getProjectManagePageInfo, {
          "pageNum": that.page,
          /*"pageSize": that.size,*/
         "pageSize": that.size,
          "search": that.search
        }
      )
      .then(function(res) {
        if(res.success) {
          that.projectList = res.pageInfo.list;
          that.total = res.pageInfo.total;
          that.page = res.pageInfo.pageNum;
        }
      })
    },
    //获取项目
    getProject: function(id) {
      let that = this;
      that.project = {};
      if (id != null) {
        that.$post(
          that.urls.getProjectInfo, {
            "id": id
          }
        )
        .then(function(res) {
          if(res.success) {
            that.project = res.data;
            that.projectName = that.project.name;
            that.uploadFileList = [that.getFileName(that.project.demandBookUrl)];
          }
        })
      }
    },
    //删除项目
    deleteProject: function(id) {
      let that = this;
      layer.confirm('你确定要删除吗?', {icon: 3, title:'提示'}, function(index){
        if (id != null) {
          that.$post(
            that.urls.deleteProjectInfo, {
              "id": id
            }
          )
          .then(function(res) {
            let msg = "";
            if(res.success) {
              msg = "删除成功！";
              //从新渲染列表数据
              that.searchTodo();
            } else {
              msg = "删除失败！";
            }
            layer.msg(msg, {
              time: 3000, //3s后自动关闭
            });
          })
        }
      });
    },
    //项目发布
    projectRelease: function(id) {
      let that = this;
      if (id != null) {
        that.$post(
          that.urls.projectRelease, {
            "id": id,
            "screenAuditStatus": 2
          }
        )
        .then(function(res) {
          let msg = "";
          if(res.success) {
            msg = "发布成功！";
            //从新渲染列表数据
            that.searchTodo();
          } else {
            msg = "发布失败！";
          }
          layer.msg(msg, {
            time: 3000, //3s后自动关闭
          });
        })
      }
    },
    //保存项目
    saveProject: function() {
		  let that = this;
		  let flg = that.validate.form();
		  if (flg) {
        let obj = that.combinationFormObject(that.validate.currentForm);
        obj["fromStatus"] = 1; //来源状态为管理员
		    that.$post(
          that.urls.addBaseProject,
          obj
        )
        .then(function(res) {
          let msg = "";
          if(res.success) {
            msg = "创建成功！";
            //从新渲染列表数据
            that.searchTodo();
            //关闭窗口
            that.modelShow = false;
          } else {
            msg = "创建失败！";
          }
          layer.msg(msg, {
            time: 3000, //3s后自动关闭
          });
        })
      }
    },
    //修改项目
    updateProject: function() {
      let that = this;
      let flg = that.validate.form();
      if (flg) {
        let obj = that.combinationFormObject(that.validate.currentForm);
        that.$post(
          that.urls.updateProjectInfo,
          obj
        )
        .then(function(res) {
          let msg = "";
          if(res.success) {
            msg = "修改成功！";
            //从新渲染列表数据
            that.searchTodo();
            //关闭窗口
            that.modelShow = false;
          } else {
            msg = "修改失败！";
          }
          layer.msg(msg, {
            time: 3000, //3s后自动关闭
          });
        })
      }
    },
    //查询所有有效基地
    searchAllAreas: function() {
      let that = this;
      that.$post(
        that.urls.getAllValidBaseAreas
      )
      .then(function(res) {
        if(res.success) {
          that.areasList = res.data;
        }
      })
    },
    //查询所有专家工作记录
    searchAllExpertRorkRecord: function (projectId,expertId) {
      let that = this;
      that.expertWorkLogList = [];
      that.$post(
        //that.urls.getAllExpertWorkLog,
        that.urls.getExpertWorkLogPageInfo,
        {
          "projectId": projectId,
          "expertId": expertId,
          "pageNum": that.workLogPage,
          "pageSize": that.workLogSize
        }
      )
      .then(function(res) {
        if(res.success) {
          //that.expertWorkLogList = res.data;
          that.expertWorkLogList = res.pageInfo.list;
          that.workLogTotal = res.pageInfo.total;
          that.workLogPage = res.pageInfo.pageNum;
        }
      })
    },
    //获取模板信息
    getTemplate: function() {
      let that = this;
		  //模板类型(0：其他;1:基地申请；2：项目申报；3：项目申请；4：合作合同；5：任务书；6：中期报告；7：结题报告)
      that.$post(
        that.urls.getTemplate, {
          "type": 2
        }
      )
      .then(function(res) {
        if(res.success) {
          that.template = res.data;
        }
      })
    },
    //项目需求书模板下载
    download: function () {
      let url = this.template.url;
      if (url == null || url == "") {
        layer.msg("请联系管理员上传【项目申报书】", {
          time: 3000, //3s后自动关闭
        });
        return;
      }
      window.location.href = this.urls.download + "?path=" + url;
    }
	}
}
